﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Macaw.Wss3.DependencySpiderSolution;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

namespace Macaw.Wss3.DependencySpiderSolution
{
    public partial class DependencySpiderJobStatus : LayoutsPageBase
    {
        protected Label lblIndexerStarted;
        protected Label lblIndexerCompleted;
        protected Label lblIndexerCount;
        protected Label lblSpiderStarted;
        protected Label lblSpiderCompleted;
        protected Label lblSpiderCount;
        protected Label lblSpiderData;
        protected Label lblDatasetSize;
        protected Repeater rptUnlinkedSPObjects;
        protected Repeater rptErrors;

        protected void Page_Load(object sender, EventArgs e)
        {
        }


        private void ShowData()
        {
            SPSite site = SPContext.Current.Site;
            DependencyMapStorageCollection storagecollection = new DependencyMapStorageCollection(site.WebApplication);

            DependencyMapStorage dependencymapstorage = storagecollection.GetValue<DependencyMapStorage>(site.ID);
            if (dependencymapstorage != null)
            {
                if (!string.IsNullOrEmpty(dependencymapstorage.dependencymap))
                {
                    DependencyMap dependencymap = new DependencyMap();
                    StringReader sr = new StringReader(dependencymapstorage.dependencymap);
                    dependencymap.ReadXml(sr);
                    sr.Close();

                    #region UnlikedSPObjects
                    List<DependencyMap.ResourceRow> unlinkedspobjects = new List<DependencyMap.ResourceRow>();
                    foreach (DependencyMap.ResourceRow resourcerow in dependencymap.Resource.Select("(SPID IS NOT NULL and error=false)", "Url"))
                    {
                        DependencyMap.DependencyRow[] referers = resourcerow.GetDependencyRowsByResource_Link_Dependency();
                        if (referers == null || referers.Length == 0)
                        {
                            unlinkedspobjects.Add(resourcerow);
                        }
                    }
                    rptUnlinkedSPObjects.DataSource = unlinkedspobjects;
                    rptUnlinkedSPObjects.DataBind();
                    #endregion

                    #region Errors
                    List<DependencyMap.ResourceRow> errors = new List<DependencyMap.ResourceRow>();
                    foreach (DependencyMap.ResourceRow resourcerow in dependencymap.Resource.Select("error=true","ContentType ASC"))
                    {
                        errors.Add(resourcerow);
                    }
                    rptErrors.DataSource = errors;
                    rptErrors.ItemDataBound += new RepeaterItemEventHandler(rptErrors_ItemDataBound);
                    rptErrors.DataBind();
                    #endregion
                }

                if (dependencymapstorage.dependencymap != null)
                {
                    lblDatasetSize.Text = (dependencymapstorage.dependencymap.Length / 1024).ToString();
                }

                #region Indexer
                if (!dependencymapstorage.IndexingStarted.Equals(DateTime.MinValue))
                {
                    lblIndexerStarted.Text = dependencymapstorage.IndexingStarted.ToString();
                }
                if (!dependencymapstorage.IndexingCompleted.Equals(DateTime.MinValue))
                {
                    lblIndexerCompleted.Text = dependencymapstorage.IndexingCompleted.ToString();
                }
                lblIndexerCount.Text = dependencymapstorage.IndexedItemCount.ToString();
                #endregion

                #region Spider
                if (!dependencymapstorage.SpideringStarted.Equals(DateTime.MinValue))
                {
                    lblSpiderStarted.Text = dependencymapstorage.SpideringStarted.ToString();
                }
                if (!dependencymapstorage.SpideringCompleted.Equals(DateTime.MinValue))
                {
                    lblSpiderCompleted.Text = dependencymapstorage.SpideringCompleted.ToString();
                }
                lblSpiderCount.Text = dependencymapstorage.SpideredItemCount.ToString();
                lblSpiderData.Text = (dependencymapstorage.SpideredData /1024).ToString();
                #endregion
            }

        }

        void rptErrors_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType== ListItemType.Item ||e.Item.ItemType== ListItemType.AlternatingItem)
            {
                HyperLink refererlink = (HyperLink ) e.Item.FindControl("Referer");
                Macaw.Wss3.DependencySpiderSolution.DependencyMap.ResourceRow resourcerow = (Macaw.Wss3.DependencySpiderSolution.DependencyMap.ResourceRow)e.Item.DataItem;
                if (refererlink!=null && resourcerow!=null)
                {
                    refererlink.NavigateUrl ="/_layouts/Macaw.Wss3.DependencySpiderSolution/UrlReferers.aspx?Url=" + HttpUtility.UrlEncode( resourcerow.Url );
                }
            }
        }

        protected override void CreateChildControls()
        {
            ShowData();

            base.CreateChildControls();
        }
    }
}
